package com.example.multidrive;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.multidrive.R.id;

public class NovaNota extends Activity {

	DatabaseSQL multidriveDb = null;
	SQLiteDatabase db = null;

	String disciplina;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nova_nota);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nova_nota, menu);

		Intent intent = getIntent();
		disciplina = intent.getStringExtra("nomeDisciplina");

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void adicionarNota(View view) {

		try {
			EditText txtTitulo = (EditText) findViewById(id.editTextTitulo);
			String titulo = txtTitulo.getText().toString();
			EditText txtCorpo = (EditText) findViewById(id.editTextCorpo);
			String corpo = txtCorpo.getText().toString();

			multidriveDb = new DatabaseSQL(getApplicationContext());
			db = multidriveDb.getWritableDatabase();

			Intent intent = getIntent();
			String disciplina = intent.getStringExtra("nomeDisciplina");

			db.execSQL("INSERT INTO notas (titulo, texto, nomeDisciplina ) "
					+ "VALUES ('" + titulo + "','" + corpo + "','" + disciplina
					+ "')");

			Toast.makeText(getApplicationContext(), "Nota criada com sucesso!" , Toast.LENGTH_LONG).show();

			db.close();
			multidriveDb.close();

			Intent i = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(i);
		} catch (Exception ex) {
			new AlertDialog.Builder(this).setMessage(ex.getMessage())
					.setPositiveButton("ERRO", null).show();
		}
	}

}
